package main

import (
	"encoding/base64"
	"flag"
	"regexp"

	"log"

	"git.oschina.net/wodadehencou/tools/lib/display"
	"git.oschina.net/wodadehencou/tools/lib/input"
)

func main() {
	flag.Parse()
	in := input.ShIn(flag.Args())
	out, err := doBase64(in)
	if err != nil {
		log.Fatalln(err)
	}
	display.Bytes(out)
}

func doBase64(in []byte) (out []byte, err error) {
	// remove lines start with -
	re1 := regexp.MustCompile(`-.*\n`)
	in = []byte(re1.ReplaceAllString(string(in), ""))

	// remove spaces
	//re2 := regexp.MustCompile(`\s`)
	//in = []byte(re2.ReplaceAllString(string(in), ""))

	out = make([]byte, base64.StdEncoding.DecodedLen(len(in)))
	_, err = base64.StdEncoding.Decode(out, in)
	if err != nil {
		log.Panicln("decode error:", err)
		return nil, err
	}
	return out, nil
}
